// src/utils/map.js

let TMap = null;

const key = "WLBBZ-MZJEQ-UKB5H-4P23X-G25T3-EYBQC";
// const key = "DHTBZ-XURYP-UH3DR-LOJX5-RM6Q7-5WBLO";

// easeTo 平滑移动
// setViewMode 切换2D/3D
// InfoWindow 信息窗口

export function loadTMap() {
  if (TMap) {
    return Promise.resolve(TMap);
  }

  return new Promise((resolve, reject) => {
    const script = document.createElement("script");
    script.src = `https://map.qq.com/api/gljs?v=1.exp&key=${key}`;
    script.async = true;
    script.onload = () => {
      TMap = window.TMap;
      resolve(TMap);
    };
    script.onerror = (error) => {
      reject(new Error("Failed to load Tencent Map"));
    };
    document.head.appendChild(script);
  });
}

export function unloadTMap() {
  if (TMap) {
    TMap = null;
    const script = document.querySelector(
      "script[src*='https://map.qq.com/api/gljs']"
    );
    if (script) {
      document.head.removeChild(script);
    }
    delete window.TMap;
  }
}
